package net.wangds.auth.resource.comp;

import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.auth.resource.repo.rdbms.ResourceJpaRepo;
import net.wangds.data.RemovedEnum;
import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.repository.RemovableRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
public class GeneralResourceComp extends AbstractRemovedComp<GeneralResourceEntity> {

    @Autowired
    private ResourceJpaRepo mmJpaRes;

    @Autowired
    private JdbcComp mmJdbc;

    protected GeneralResourceComp(RemovableRepo<GeneralResourceEntity> repo) {
        super(repo);
    }

    @Override
    public Page<GeneralResourceEntity> findByKeywords(String keywords, Pageable paging) {
        String sql = "SELECT t1.* \n" +
                "FROM auth_resource t1\n" +
                "where t1.removed = 0 and \n" +
                "( t1.resource_label regexp ? or t1.resource_desc regexp ?)";
        return mmJdbc.executeQuery(sql, new Object[]{keywords, keywords}, paging, GeneralResourceEntity.class);

    }


    public List<GeneralResourceEntity> findBySchemaAndSys(ResourceSchemaEnum schema, String sys, Sort sort) {
        return mmJpaRes.findBySchemaAndSys(schema, sys, sort);
    }

    public List<GeneralResourceEntity> findBySchema(ResourceSchemaEnum schema, Sort sort) {
        return mmJpaRes.findBySchema(schema, sort);
    }

    public List<GeneralResourceEntity> findBySchemaAndSysAndRemoved(ResourceSchemaEnum menu, String system, RemovedEnum normal, Sort sort) {
        return mmJpaRes.findBySchemaAndSysAndRemoved(menu, system, normal, sort);
    }

}
